//
//  HGPageTitleView.m
//  PageController
//
//  Created by yu on 2018/4/6.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "HGPageTitleView.h"
#import "UIColor+Extension.h"
#define kScrollLineH 2.0
#define RGBColor(r,g,b) [UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:1]
#define kSelectColor RGBColor(255, 128, 0)
//[UIColor colorWithRed:(255 / 255.0) green:(128 / 255.0) blue:(0 / 255.0) alpha:1]
#define kNormalColor RGBColor(85, 85, 85)
//[UIColor colorWithRed:(85 / 255.0) green:(85 / 255.0) blue:(85 / 255.0) alpha:1]
@interface HGPageTitleView()
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)NSArray *titles;
@property (nonatomic, strong)NSMutableArray *titleLabels;
@property (nonatomic, strong)UIView *scrollLine;
@property (nonatomic, strong)UIView *bottomLine;
@property (nonatomic, assign)NSInteger currentLabelIndex;
@end

@implementation HGPageTitleView

- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles {
    if (self = [super initWithFrame:frame]) {
        _titles = titles;
        _currentLabelIndex = 0;
        [self setupUI];
    }
    return self;
}

- (NSMutableArray *)titleLabels {
    if (_titleLabels == nil) {
        _titleLabels = [NSMutableArray new];
    }
    return _titleLabels;
}

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollsToTop = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

#pragma mark -- 设置UI
- (void)setupUI {
    //添加UIScrollView
    [self addSubview:self.scrollView];
    //添加title对应的label
    [self setupTitleLabels];
    //添加底线和滑块
    [self setupBottomLineAndScrollLine];
}


- (void)setupTitleLabels {
    
    CGFloat labelW = self.frame.size.width / self.titles.count;
    CGFloat labelH = self.frame.size.height - kScrollLineH;
    CGFloat labelY = 0;
    
    for (int i = 0; i < self.titles.count; i++) {
        CGFloat labelX = labelW * i;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelW, labelH)];
        label.text = self.titles[i];
        label.textColor = kNormalColor;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:16.0];
        label.tag = i;
        [self.scrollView addSubview:label];
        [self.titleLabels addObject:label];
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped:)];
        [label addGestureRecognizer:tapGes];
    }
    
}

- (void)setupBottomLineAndScrollLine {
    _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5)];
    _bottomLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_bottomLine];
    
    UILabel *firstLabel = self.titleLabels.firstObject;
    firstLabel.textColor = kSelectColor;
    
    _scrollLine = [[UIView alloc] initWithFrame:CGRectMake(firstLabel.frame.origin.x, self.frame.size.height - kScrollLineH, firstLabel.frame.size.width, kScrollLineH)];
    _scrollLine.backgroundColor = kSelectColor;
    [self addSubview:_scrollLine];

}


#pragma mark -- Label点击事件
- (void)labelTapped:(UITapGestureRecognizer *)tapGes {
    
    UILabel *currentLabel = (UILabel *)tapGes.view;
    UILabel *oldLabel = self.titleLabels[_currentLabelIndex];
    
    currentLabel.textColor = [UIColor orangeColor];
    oldLabel.textColor = [UIColor darkGrayColor];
    
    _currentLabelIndex = currentLabel.tag;
    
    [UIView animateWithDuration:0.15 animations:^{
        CGRect newFrame = _scrollLine.frame;
        newFrame.origin.x = currentLabel.frame.origin.x;
        _scrollLine.frame = newFrame;
    }];
    
    if ([self.delegate respondsToSelector:@selector(pageTitleView:selectedIndex:)]) {
        [self.delegate pageTitleView:self selectedIndex:_currentLabelIndex];
    }
    
}


#pragma mark -- 外部API
//接收滑动的方法
- (void)setTitleViewWithProgress:(CGFloat)progress sourceIndex:(NSInteger)sourceIndex targetIndex:(NSInteger)targetIndex {
    
    //1.取出sourceLabel/targetLabel
    UILabel *sourceLabel = self.titleLabels[sourceIndex];
    UILabel *targetLabel = self.titleLabels[targetIndex];
    
    //2.处理滑块的逻辑
    CGFloat moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x;
    CGRect newFrame = self.scrollLine.frame;
    newFrame.origin.x = sourceLabel.frame.origin.x + moveTotalX * progress;
    self.scrollLine.frame = newFrame;
 
    //3.颜色的渐变
    //选中的颜色RGB:(255,128,0)
    //未选中的颜色RGB:(85,85,85)
    //颜色的渐变范围
//    CGFloat redDelta = 255 - 85;
//    CGFloat greenDelta = 128 - 85;
//    CGFloat blueDelta = 0 - 85;
    UIColor *normalColor = [UIColor red:85 green:85 blue:85];
    UIColor *selectColor = [UIColor red:255 green:128 blue:0];
    
    
    sourceLabel.textColor = [self countColorWithFromRGBColor:selectColor toRGBColor:normalColor andProgress:progress];
    targetLabel.textColor = [self countColorWithFromRGBColor:normalColor toRGBColor:selectColor andProgress:progress];
    
    _currentLabelIndex = targetIndex;
}


- (UIColor *)countColorWithFromRGBColor:(UIColor *)sourceColor toRGBColor:(UIColor *)targetColor andProgress:(CGFloat)progress {
    
    CGFloat redDelta = targetColor.red - sourceColor.red;
    CGFloat greenDelta = targetColor.green - sourceColor.green;
    CGFloat blueDelta = targetColor.blue - sourceColor.blue;
    
    CGFloat red = sourceColor.red + redDelta * progress;
    CGFloat green = sourceColor.green + greenDelta * progress;
    CGFloat blue = sourceColor.blue + blueDelta * progress;
    
    
    return [UIColor red:red green:green blue:blue];
}


@end










